package fatcat.snowberry.diagram;

import java.awt.Frame;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IPersistableElement;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;

import fatcat.gui.snail.SnailShell;
import fatcat.snowberry.diagram.dp.DPFrame;


public class DiagramEditor2 extends EditorPart {
	
	Composite diagramComposite = null;
	LoadingFrame loadingFrame = null;
	DPFrame dpFrame = null; // will be initialized in LoadingFrame
	DiagramFrame diagramFrame = null; // will be initialized in LoadingFrame
	SnailShell snailShell = null;
	DiagramEditorToolbar toolbar = null;
	IFile file = null;
	IFileEditorInput fileInput = null;

	@Override
	public void doSave(IProgressMonitor monitor) {

	}

	@Override
	public void doSaveAs() {

	}
	
	public final SnailShell getSnailShell() {
		return snailShell;
	}
	
	public final DiagramFrame getDiagramFrame() {
		return diagramFrame;
	}
	
	public final Composite getDiagramComposite() {
		return diagramComposite;
	}

	@Override
	public void init(IEditorSite site, IEditorInput input) throws PartInitException {
		if (!(input instanceof IFileEditorInput)) {
			throw new PartInitException("Java类图编辑器只能用于编辑Java源代码文件。");
		} else {
			fileInput = ((IFileEditorInput) input);
			file = fileInput.getFile();
			if (!file.getFileExtension().equals("java")) {
				throw new PartInitException("Java类图编辑器只能用于编辑Java源代码文件。");
			}
			
			setPartName(file.getName() + " [类图]");
			setTitleToolTip(file.getName() + " [类图]");
			// TODO （低优先）加上编辑器图标
//			setTitleImage()
			setSite(site);
			
			// 这个编辑器的input经过了加工，使得其input与其他input比较时均不相同（覆盖了equals方法）。
			// 这样一来就可以使用openEditor方法用其他编辑器打开这个文件了（fileInput成员保存了原始的input）。
			setInput(new IFileEditorInput() {
				
				@Override
				public boolean equals(Object obj) {
					return false;
				}

				@Override
				public IFile getFile() {
					return fileInput.getFile();
				}

				@Override
				public IStorage getStorage() throws CoreException {
					return fileInput.getStorage();
				}

				@Override
				public boolean exists() {
					return fileInput.exists();
				}

				@Override
				public ImageDescriptor getImageDescriptor() {
					return fileInput.getImageDescriptor();
				}

				@Override
				public String getName() {
					return fileInput.getName();
				}

				@Override
				public IPersistableElement getPersistable() {
					return fileInput.getPersistable();
				}

				@Override
				public String getToolTipText() {
					return fileInput.getToolTipText();
				}

				@SuppressWarnings("unchecked")
				@Override
				public Object getAdapter(Class adapter) {
					return fileInput.getAdapter(adapter);
				}
				
			});
		}
	}
	
	public boolean isReady() {
		return diagramFrame != null;
	}

	@Override
	public boolean isDirty() {
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}

	@Override
	public void createPartControl(final Composite parent) {
		toolbar = new DiagramEditorToolbar(parent, this);
		toolbar.setVisible(true);
		diagramComposite = new Composite(parent, SWT.EMBEDDED);
		Frame frame = SWT_AWT.new_Frame(diagramComposite); // TODO （低优先）应当在frame中新建一个Panel做为顶层容器
		snailShell = new SnailShell(frame, 30);
		loadingFrame = new LoadingFrame(snailShell, this);
		loadingFrame.show();
		loadingFrame.showDiagram();
		
		// layout
		parent.setLayout(null);
		parent.addControlListener(new ControlAdapter() {
			
			final int TOOL_BAR_HEIGHT = 24;
			
			@Override
			public void controlResized(ControlEvent e) {
				
				toolbar.setBounds(0, 0, parent.getSize().x, TOOL_BAR_HEIGHT);
				diagramComposite.setBounds(0, TOOL_BAR_HEIGHT, parent.getSize().x, parent.getSize().y - TOOL_BAR_HEIGHT);
			}
		});
	}

	@Override
	public void setFocus() {
		diagramComposite.setFocus();
	}

	@Override
	public void dispose() {
		if (diagramFrame != null)
			diagramFrame.dispose();
		if (snailShell != null)
			snailShell.getFramework().dispose();
		super.dispose();
	}
}
